{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 1. 其他类型转字符串型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'str'> <class 'int'>\n",
      "我叫张三，今年20岁\n"
     ]
    }
   ],
   "source": [
    "name = '张三'\n",
    "age = 20\n",
    "\n",
    "print(type(name), type(age))  # 说明name与age的数据类型不同\n",
    "# print('我叫' + name + '，今年' + age + '岁')  # +为连接符，将不同类型的数据连接的时候会报错，解决方法：类型转换\n",
    "print('我叫' + name + '，今年' + str(age) + '岁')  # 将int类型通过str()函数转成了str类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "------------str()将其他类型转成str类型------------\n",
      "<class 'int'> <class 'float'> <class 'bool'>\n",
      "10 198.8 False <class 'str'> <class 'str'> <class 'str'>\n"
     ]
    }
   ],
   "source": [
    "print('------------str()将其他类型转成str类型------------')\n",
    "a = 10\n",
    "b = 198.8\n",
    "c = False\n",
    "print(type(a), type(b), type(c))\n",
    "print(str(a), str(b), str(c), type(str(a)), type(str(b)), type(str(c)))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 2. 其他类型转整数型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "------------int()将其他类型转成int类型------------\n",
      "<class 'str'> <class 'float'> <class 'str'> <class 'bool'> <class 'str'>\n",
      "128 <class 'int'>\n",
      "98 <class 'int'>\n",
      "1 <class 'int'>\n"
     ]
    }
   ],
   "source": [
    "print('------------int()将其他类型转成int类型------------')\n",
    "s1 = '128'\n",
    "f1 = 98.7\n",
    "s2 = \"76.77\"\n",
    "ff = True\n",
    "s3 = 'hello'\n",
    "print(type(s1), type(f1), type(s2), type(ff), type(s3))\n",
    "print(int(s1), type(int(s1)))  # 将str转成int，前提是字符串为数字串\n",
    "print(int(f1), type(int(f1)))  # float转成int，截取整数部分，舍弃小数部分\n",
    "# print(int(s2), type(int(s2)))  # 将str转成int类型，报错，因为字符串为小数串\n",
    "print(int(ff), type(int(ff)))  # 将bool类型转成对应的数字\n",
    "# print((int(s3), type(int(s3))))  # str转为int，字符串必须为数字串，且必须为整数串"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 3. 其他类型转浮点型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "------------float()将其他类型转成float类型------------\n",
      "<class 'str'> <class 'str'> <class 'bool'> <class 'str'> <class 'int'>\n",
      "128.98 <class 'float'>\n",
      "76.0 <class 'float'>\n",
      "1.0 <class 'float'>\n",
      "98.0 <class 'float'>\n"
     ]
    }
   ],
   "source": [
    "print('------------float()将其他类型转成float类型------------')\n",
    "s4 = '128.98'\n",
    "s5 = \"76\"\n",
    "fff = True\n",
    "s6 = 'hello'\n",
    "i = 98\n",
    "print(type(s4), type(s5), type(fff), type(s6), type(i))\n",
    "print(float(s4), type(float(s4)))\n",
    "print(float(s5), type(float(s5)))  # int转为float，后面加.0\n",
    "print(float(fff), type(float(fff)))  # 后面加.0\n",
    "# print(float(s6), type(float(s6)))  # 报错，str里面是字符串不能转为float\n",
    "print(float(i), type(float(i)))  # int转为float，后面加.0"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.6.3",
   "language": "python",
   "name": "python3.6.3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": false,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
